import { Component, OnInit } from '@angular/core';
import { Course } from '../../model/course';
import { Student } from '../../model/student';
import { Book } from '../../model/book';
import { CourseService } from '../../service/course.service';
import { CourseResult } from '../../model/courseresult';
import { Classroom } from '../../model/classroom';
import { Teacher } from '../../model/teacher';

@Component({
  selector: 'app-courseindex',
  templateUrl: './courseindex.component.html',
  styleUrls: ['./courseindex.component.css']
})
export class CourseindexComponent implements OnInit {
  c:Course
  cl:Array<Course>
  sl:Array<Student>
  bl:Array<Book>
  rl:Array<Classroom>
  tl:Array<Teacher>
  constructor(private courseservice:CourseService) {
    this.c=new Course();
   }

  ngOnInit() {
   this.initCourse()
   //this.setCourse()
   this.initStudent();
   this.initBook();
   this.initClassroom();
   this.initTeacher(); 

  }
  //  initCourse(){
  //    this.cl=new Array();
  //    for(let i=0;i<25;i++){
  //      let c=new Course()
  //      c.id=i;
  //   this.cl.push(c)
  //    }
  //  }

  //  setCourse(al:Array<CourseResult>){
  //   this.cl= new Array(); 
  //   for(let x of al){
  //      //this.cl[x.id]=x
  //      let t =new Course();
  //      t.classroom=x.classroom;
  //      t.name=x.name;
  //      t.student=x.student;
  //      t.teacher=x.teacher;
  //      this.cl[x.cid]=t
  //    }
  //  }

  initCourse(){
    this.cl = new Array();
    let temparray = new Array()
    for(let i=0 ;i<25;i++){
     let c = new Course()
     c.id= i;
     this.cl.push(c);
    }
    this.courseservice.getcourseList().then((data:any)=>{
      if (data != null ) {
        temparray = data
              for(let i of temparray){
                this.cl[i.cid] = i
              }
       }
    })
   }

   setCourse(searchresult:Array<CourseResult>){
    this.cl = new Array();
    for(let x of searchresult){
    
      let t = new Course();
      t.classroom= x.classroom
      //t.id=x.cid
      t.name= x.name
      t.student= x.student
      t.teacher= x.teacher
     this.cl[x.cid]=t;
    }
   }

   initStudent(){
    this.sl=new Array();
    this.courseservice.getStudentList().then((data:any)=>{
      this.sl=data;
    })
   }
   onStudentSelect(s:Student){
    console.dir(s);
    this.courseservice.getCourseListByParm('student',s.name).then((data:any)=>{
     console.dir(data);
     this.setCourse(data);
   })
  }

   initBook(){
    this.bl = new Array();
   this.courseservice.getBookList()
   .then((data:any)=>{
     this.bl= data;
   })
  }

  onBookSelect(xb:Book){
    console.dir(xb);
    this.courseservice.getCourseListByParm('book',xb.id)
    .then((data:any)=>{
      console.dir(data);
      this.setCourse(data);
    })
  
   }


   initClassroom(){
    this.rl=new Array();
   this.courseservice.getClassroomList().then((data:any)=>{
     this.rl=data;
   })
  }

  onClassSelect(r:Classroom){
    console.dir(r);

    this.courseservice.getCourseListByParm('classroom',r.name).then((data:any)=>{
      console.dir(data);

      this.setCourse(data);
    })
  }

  initTeacher(){
     this.tl=new Array();
   this.courseservice.getTeacherList().then((data:any)=>{
     this.tl=data;
   })
    }
  
    onTeacherSelect(t:Teacher){
      console.dir(t);
      this.courseservice.getCourseListByParm('teacher',t.name).then((data:any)=>{
        console.dir(data);
        this.setCourse(data);
      })
    }

}
